<template>
  <div class="pushManagement">
    <MetaSearchForm @on-search="tableSearch">
      <el-form ref="pushManagement_form" :model="searchData" label-width="120px" class="pushManagement_searchFrom">
        <el-form-item label="用户id" class="pushManagement_searchFromItem">
          <el-input v-model="searchData.userId" placeholder="请输入用户id"></el-input>
        </el-form-item>
        <el-form-item label="点位id" class="pushManagement_searchFromItem">
          <el-input v-model="searchData.pointId" placeholder="请输入点位id"></el-input>
        </el-form-item>
      </el-form>
    </MetaSearchForm>
    <MetaPageTable
      ref="pushManagementTable"
      class="pushManagement_table"
      table-title="推送列表"
      :is-auto-req="true"
      :req-url="reqUrl"
      :req-data="searchDataForTable"
      req-method="post"
      :display-item="displayItem"
      :btn-group="btnGroup"
      :line-btn-group="lineBtnGroup"
      @btnClick="tableBtnClick"
      @lineBtnClick="tableInlineBtnClick"
    />
    <el-dialog
      v-if="customerDialogFlag"
      :title="itemEditData.id && itemEditData.id !== '' ? '编辑推送项' : '新建推送项'"
      :visible.sync="customerDialogFlag"
      width="60%"
      modal
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false">
      <el-form ref="tCustomerEdit_form" :model="itemEditData" label-width="150px" :rules="editRules">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="itemEditData.userId" placeholder="请输入用户id" :disabled="itemEditData.userId !== ''"></el-input>
        </el-form-item>
        <el-form-item label="点位id" prop="pointId">
          <el-input v-model="itemEditData.pointId" placeholder="请输入点位id" :disabled="itemEditData.pointId !== ''"></el-input>
        </el-form-item>
        <el-form-item label="推送地址" prop="domainKey">
          <el-select
            v-model="itemEditData.domainKey"
            placeholder="请选择推送地址"
            clearable
            filterable
            style="width: 80%"
            @change="showUserName">
            <el-option
              v-for="item in domainList"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialogFooter">
        <el-button size="mini" @click="editDialogClose()">取消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="editDialogSubmit()"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import MetaPageTable from '../components/MetaPageTable'
import MetaSearchForm from '../components/MetaSearchForm'
import deepClone from '../utils/deepClone'
import { validateLetterNumber } from '../utils/validate'
export default {
  name: 'pushManagement',
  components: {
    MetaPageTable,
    MetaSearchForm
  },
  data () {
    return {
      customerDialogFlag: false,
      reqUrl: process.env.PROGOO_GATE_WATER + 'pointEquipment/userPointList',
      itemEditData: {
        id: '',
        userId: '',
        pointId: '',
        domainKey: '',
      },
      domainList: [],
      searchData: {
        pointId: '',
        userId: '',
      },
      searchDataForTable: {},
      editRules: {
        // domainKey: [
        //   { required: true, message: '请选择推送地址', trigger: 'blur' },
        // ]
      },
      displayItem: [
        {
          label: '用户id',
          prop: 'userId',
          width: "350",
        },
        {
          label: '用户',
          prop: 'userName',
          width: "150",
        },
        {
          label: '点位id',
          prop: 'pointId',
          width: "350",
        },
        {
          label: '点位',
          prop: 'pointName',
          width: "150",
        },
        {
          label: '推送地址',
          prop: 'domainKey'
        }
      ],
      btnGroup: [
      ],
      lineBtnGroup: [
        {
          id: 'edit',
          title: '编辑',
        }
      ],
    }
  },
  mounted() {
    // 初始化
    this.getDomains();
  },
  methods: {
    getDomains() {
      this.$fetch({
        url: process.env.PROGOO_GATE_WATER + "domains/group",
        method: "post"
      }).then((res) => {
        if (res.data.code === 200) {
          console.log(res.data.result)
          this.domainList = res.data.result;
        } else {
          this.$notify.error({
            title: "错误",
            message: res.data.message,
          });
        }
      });
    },
    showUserName(e) {
      for (let i in this.domainList) {
        if (e == this.domainList[i].id) {
          this.searchData = this.domainList[i];
        }
      }
    },
    tableBtnClick(item) {

    },
    tableInlineBtnClick(item) {
      if (item.button === 'edit') {
        this.itemEditData = deepClone(item.rows)
        this.itemEditData.isInactive = this.itemEditData.isInactive + ''
        this.customerDialogFlag = true
      }
    },
    tableSearch() {
      let param = {}
      for(let key in this.searchData) {
        if(key === 'dateBetween') {
          param.datestart = this.searchData[key] && this.searchData[key].length > 0 ? this.searchData[key][0] + ' 00:00:00' : ''
          param.dateend =  this.searchData[key] && this.searchData[key].length > 0 ? this.searchData[key][1] + ' 23:59:59' : ''
        }else {
          param[key] = this.searchData[key]
        }
      }
      this.searchDataForTable = param
      this.$nextTick(() => {
        this.$refs.pushManagementTable.formDataInit(true)
      })

    },
    editDialogSubmit() {
      this.$refs['tCustomerEdit_form'].validate((valid) => {
        if (valid) {
          let params = {}
          let url = process.env.PROGOO_GATE_WATER + "pointEquipment/saveUserPoint"
          if(this.itemEditData.id !== '') {
            params.id = this.itemEditData.id
            url = process.env.PROGOO_GATE_WATER + "pointEquipment/saveUserPoint"
          }
          params.userId = this.itemEditData.userId
          params.pointId = this.itemEditData.pointId
          params.domainKey = this.itemEditData.domainKey
          this.$fetch({
            url: url,
            method: "post",
            data: params
          }).then(res => {
            if (res.data.code === 200) {
              this.editDialogClose()
              this.$nextTick(() => {
                this.$refs.pushManagementTable.formDataInit()
              })
            }else {
              this.$notify.error({
                title: '错误',
                message: res.data.message
              });
            }
          })
        } else {
          return false;
        }
      });
    },
    editDialogClose() {
      this.itemEditData = {
        id: '',
        userId: '',
        pointId: '',
        domainKey: ''
      }
      this.customerDialogFlag = false
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less">
.pushManagement{
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 15px;
  overflow-y: scroll;
  .pushManagement_table {
    width: 100%;
    overflow-y: unset;
  }
  .dataDetailsForm_item {
    line-height: normal;
    padding: 8px 0;
  }
  .pushManagement_searchFrom {
    width: 100%;
  }
  .pushManagement_searchFromItem {
    float: left;
  }
  .dialogFooter {
    display: flex;
    justify-content: center;
    margin-top: 0.5rem;
  }
}
</style>
